package cn.cmcc.s2a.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.opensymphony.xwork2.ActionSupport;
import static com.opensymphony.xwork2.Action.*;

@Results( { @Result(name = SUCCESS, params = { "contentType",
		"application/octet-stream;charset=ISO8859-1", "inputName",
		"inputStream", "contentDisposition", "attachment;filename=${fileName}",
		"bufferSize", "4096" }, type = "stream") })
public class DownloadAction extends ActionSupport {

	private static final long serialVersionUID = 2714380492481657451L;
	private String fileName = null;

	public String getFileName() {
		try {
			fileName = new String(fileName.getBytes(), "ISO-8859-1");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return fileName;
	}

	public InputStream getInputStream() {
		String rootFile = DownloadAction.class.getResource("/").getPath();
		File f = new File(rootFile + "resultFile/"+fileName);
		String realPath = f.getAbsolutePath();
		LOG.info("realPath:---------"+realPath);
		InputStream in = null;
		try {
			in = new FileInputStream(new File(realPath));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return in;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public String execute() {
		return SUCCESS;
	}
}
